package com.ruoyi.service.jpjs.empchange.impl;

import com.ruoyi.common.config.Global;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.mapper.jpjs.PublicMapper;
import com.ruoyi.mapper.jpjs.empchange.EmpChangeMapper;
import com.ruoyi.service.jpjs.PublicService;
import com.ruoyi.service.jpjs.empchange.EmpChangeService;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.util.BaseResult;
import com.ruoyi.util.ResultEnum;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class EmpChangeServiceimpl implements EmpChangeService {

    @Autowired
    EmpChangeMapper empChangeMapper;

    @Autowired
    PublicMapper publicMapper;

    @Autowired
    PublicService publicService;

    @Override
    public List<Map<String, Object>> list(Map map) {
        List<Map<String, Object>> compList = publicService.findUserCompAndJszb();
        map.put("compList", compList);
        return empChangeMapper.list(map);
    }

    @Override
    public Map<String, Object> findDataById(String id) {
        return empChangeMapper.findDataById(id);
    }

    @Override
    public int add(Map map) {
        //在人员异动里增加
        int num = 0;
        num += empChangeMapper.add(map);
        if (num > 0) {
            String oid = empChangeMapper.getIdByMonthYear(map);
            empChangeMapper.addOnJob(oid);
            empChangeMapper.addQuit(oid);
            empChangeMapper.addHuman(oid);
            empChangeMapper.addCzlrfy(oid);
        }
        //empChangeMapper.addOnJob(map);
        //empChangeMapper.addQuit(map);
        return num;

    }

    @Override
    public int edit(Map map) {
        return empChangeMapper.edit(map);
    }

    @Override
    public int remove(String ids) {
        String[] idSplit = ids.split(",");
        int num = 0;
        for (String id : idSplit) {
            num += empChangeMapper.remove(id);
        }
        return num;
    }

    @Override
    public BaseResult findCompMonthIsNull(Map map) {
        int count = empChangeMapper.findCompMonthIsNull(map);
        if (count == 0) {
            return BaseResult.build(ResultEnum.SUCCESS);
        } else {
            return BaseResult.build(ResultEnum.EMP_CHANGE_IS_NOT_NULL);
        }
    }

    @Override
    public BaseResult findMonthEndNumByYearMonth(Map map) {
        Integer monthEndNum = empChangeMapper.findMonthEndNumByYearMonth(map);
        if (monthEndNum != null) {
            return BaseResult.build(ResultEnum.SUCCESS, monthEndNum);
        } else {
            return BaseResult.build(ResultEnum.FAILURE);
        }
    }

    @Override
    public BaseResult findQuitCountByYearMonth(Map map) {
        int QuitCount = empChangeMapper.findQuitCountByYearMonth(map);
        if (QuitCount > 0) {
            return BaseResult.build(ResultEnum.SUCCESS, QuitCount);
        } else {
            return BaseResult.build(ResultEnum.FAILURE);
        }
    }

    @Override
    public String findOid(String years, String months, String compId) {
        return empChangeMapper.findOid(years,months,compId);
    }

    @Override
    public int monthEndNum(String id) {
        return empChangeMapper.monthEndNum(id);
    }
}
